package com.springboot.demo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.demo.core.utils.RedisUtils;
import com.springboot.demo.module.entity.TSysUser;
import com.springboot.demo.module.service.IUserService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 测试 -- redis 工具类
 *
 * @author Francis
 * @version 1.0
 * @date 2019-07-05 14:27
 */
public class Demo3 extends DemoApplicationTests{

    @Autowired
    IUserService userService;

    /**
     * 1、 测试 -- 存储
     */
    @Test
    public void method1(){
        TSysUser user = userService.list(null).get(0);

        RedisUtils.save("str1", "测试工具类存储");
        RedisUtils.save("str2", "测试工具类存储，并设置失效时间", 30, TimeUnit.SECONDS);

        RedisUtils.save("user1", user);
        RedisUtils.save("user2", user, 30, TimeUnit.SECONDS);

        RedisUtils.save("userJson1", user, TSysUser.class);
        RedisUtils.save("userJson2", user, TSysUser.class, 30, TimeUnit.SECONDS);

    }


    /**
     * 2、 测试 -- 键值对-取出
     */
    @Test
    public void method2(){
        String str1 = RedisUtils.getKey4String("str1");
        String str2 = RedisUtils.getKey4String("str2");

        System.out.println(str1);
        System.out.println(str2);
    }


    /**
     * 3、 测试 -- 对象取出
     */
    @Test
    public void method3(){
        TSysUser user1 = RedisUtils.getKey4Pojo("user1", TSysUser.class);
        TSysUser user2 = RedisUtils.getKey4Pojo("user2", TSysUser.class);

        System.out.println(user1);
        System.out.println(user2);
    }


    /**
     * 4、 测试 -- json取出并转为对象
     */
    @Test
    public void method4(){
        TSysUser user1 = RedisUtils.getKey4Json("userJson1", TSysUser.class);
        TSysUser user2 = RedisUtils.getKey4Json("userJson2", TSysUser.class);

        System.out.println(user1);
        System.out.println(user2);
    }

}
